package com.hbzd.protection.service;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.hbzd.protection.entity.User;
import com.hbzd.protection.mapper.GiscarjbMapper;
import com.hbzd.protection.mapper.UserMapper;

@Service
@Transactional
public class UserServiceImpl {

	@Resource
	private UserMapper userMapper;
	@Resource
	private GiscarjbMapper giscarjbMapper;

	/**
	 * 判断该openid是否已经绑定
	 * @param openid
	 * @return
	 */
	public User getByOpenId(String openid) {
		User user = userMapper.getByOpenId(openid);
		return user;
	}
	/**
	 * 查找数据库中是否含有该用户
	 * @param user
	 * @return
	 */
	public User get(String phone) {
		return userMapper.getByPhone(phone);
	}
	
	/**
	 * 添加用户
	 * @param user
	 * @return
	 */
	public void update(User user) {
		//1.判断该用户是否存在车辆表中
		Integer count = giscarjbMapper.getCount(user.getPhone());
		if(count == null || count < 1){
			user.setFlag(0);
		}else{
			user.setFlag(1);
		}
		User temp = userMapper.getByOpenId(user.getOpenid());
		if(temp != null){
			userMapper.update(temp);
		}else{
			userMapper.addUser(user);
		}
	}
	public Integer getNum(String openid) {
		Integer num = userMapper.getNum(openid);
		return num == null?0:num;
	}
	/**
	 * 记录联系购买时间
	 * @param openid
	 */
	public void updatePurchaseTime(String openid) {
		userMapper.updatePurchaseTime(openid);
	}
}
